<!--
 * @Author: your name
 * @Date: 2020-11-17 11:12:52
 * @LastEditTime: 2021-01-15 14:33:56
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /vue-next/Users/duodian/Documents/qugame/work-admin/src/views/equip/list.vue
-->
<template>
  <div>
    <div class="query">
      <el-form :inline="true" :model="query" class="demo-form-inline">
        <el-form-item label="关键词">
          <el-input
            clearable
            v-model="query.keyword"
            @keyup.enter.native="search"
            placeholder="订单id/上号密钥/下单人姓名/电话"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="query.orderStatus" placeholder="请选择">
            <el-option
              v-for="item in orderStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="游戏">
          <el-select v-model="query.gameId" placeholder="请选择">
            <el-option
              v-for="item in games"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="下单时间">
          <el-date-picker
            v-model="query.orderTime"
            align="right"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="渠道">
          <el-select v-model="query.platformType" placeholder="请选择">
            <el-option
              v-for="item in platformType"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作人">
          <el-input
            clearable
            v-model="query.operateUserName"
            placeholder="操作人"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%"
      @sort-change="sort"
      :row-class-name="tableRowClassName"
    >
      <el-table-column prop="orderNo" label="订单id" width="150">
      </el-table-column>
      <el-table-column prop="createTimeDesc" label="创建时间" width="150">
      </el-table-column>
      <el-table-column prop="secret" label="密钥" width="150">
      </el-table-column>
      <el-table-column prop="orderInfo" label="订单信息" width="340">
        <template slot-scope="scope">
          <div>
            渠道：{{ scope.row.orderInfo.platformTypeDesc }} <br />
            游戏：{{ scope.row.orderInfo.gameName }} <br />
            平台：{{ scope.row.orderInfo.areaType === 1 ? "Android" : "iOS" }}
            <br />
            订单编号: {{ scope.row.orderInfo.thirdOrderNo }} <br />
            收货人：{{ scope.row.orderInfo.receiveUserName }} <br />
            电话：{{ scope.row.orderInfo.receiveUserPhone }} <br />
            下单金额：{{ scope.row.orderInfo.paymentPrice }} 元 <br />
            下单时间：{{ scope.row.orderInfo.thirdOrderTime }} <br />
            支付方式：{{ scope.row.orderInfo.paymentTypeDesc }} <br />
            商品名称：{{ scope.row.orderInfo.goodsName }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="peaceSkinWorthVoList" label="发货信息" width="180">
        <template slot-scope="scope">
          <div>
            发货时间：{{ scope.row.orderSendInfo.sendTimeDesc }} <br />
            选择皮肤：{{ scope.row.orderSendInfo.skinName }} <br />
            匹配账号：{{ scope.row.orderSendInfo.accountName }} <br />
            <router-link
              target="_blank"
              :to="`${
                scope.row.orderInfo.gameName === '王者荣耀'
                  ? `/wzry/accDetail?accountId=${scope.row.orderSendInfo.accountNo}&dataId=${scope.row.dataId}`
                  : `/hpjy/accManageDetail?dataId=${scope.row.dataId}`
              }`"
              >匹配QQ号：{{
                scope.row.orderSendInfo.accountNo || "-"
              }}</router-link
            ><br />
            操作人：{{ scope.row.orderSendInfo.operateUserName }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="peaceSkinWorthVoList" label="体验时间" width="180">
        <template slot-scope="scope">
          <div>
            启动：{{ scope.row.startTimeDesc }} <br />
            结束：{{ scope.row.endTimeDesc }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="peaceSkinWorthVoList" label="状态" width="100">
        <template slot-scope="scope">
          <div>
            {{ orderStatusMap[scope.row.orderStatus] }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" class-name="btns">
        <template slot-scope="scope">
          <el-button
            type="success"
            size="small"
            plain
            @click="toDetail(scope.row)"
            >详情</el-button
          >
          <br />
          <el-button
            type="primary"
            size="small"
            plain
            @click="showAddTime(scope.row)"
            v-if="[COMPLETED, CANCELED, REFUND].indexOf(scope.row.orderStatus) === -1"
            >加时间</el-button
          >
          <br v-if="[COMPLETED, CANCELED, REFUND].indexOf(scope.row.orderStatus) === -1" />
          <el-button
            type="warning"
            size="small"
            plain
            v-if="[COMPLETED, REFUND].indexOf(scope.row.orderStatus) === -1"
            @click="cancelOrder(scope.row, () => toOrder(scope.row))"
            >重新分配账号</el-button
          >
          <br v-if="[COMPLETED, REFUND].indexOf(scope.row.orderStatus) === -1" />
          <el-button
            type="danger"
            size="small"
            plain
            @click="refundOrder(scope.row)"
            v-if="scope.row.orderStatus === COMPLETED"
            >退款</el-button
          >
          <br v-if="scope.row.orderStatus === COMPLETED" />
          <el-button
            type="danger"
            size="small"
            plain
            @click="cancelOrder(scope.row)"
            v-if="[COMPLETED, CANCELED, REFUND].indexOf(scope.row.orderStatus) === -1"
            >取消订单</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="tools" v-if="listTotal > query.pageSize">
      <el-pagination
        @size-change="sizeChange"
        @current-change="pageChange"
        :current-page="query.pageNum"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="listTotal"
      >
      </el-pagination>
    </div>
    <el-dialog
      title="加时长"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <div>
        <el-input
          type="number"
          v-model="minute"
          placeholder="请输入时长（分钟）"
        ></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTime">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Modal from './modal'

const WAIT_UNLOCK = 0
const ALREADY_UNLOCK = 1
const EXPERIENCING = 2
const COMPLETED = 3
const CANCELED = 4
const REFUND = 5

const orderStatusMap = {
  [WAIT_UNLOCK]: '待解锁',
  [ALREADY_UNLOCK]: '已解锁',
  [EXPERIENCING]: '体验中',
  [COMPLETED]: '已完成',
  [CANCELED]: '已取消',
  [REFUND]: '已退款'
}

const orderStatus = [
  {
    label: '全部',
    value: null
  },
  {
    label: '待解锁',
    value: WAIT_UNLOCK
  },
  {
    label: '已解锁',
    value: ALREADY_UNLOCK
  },
  {
    label: '体验中',
    value: EXPERIENCING
  },
  {
    label: '已完成',
    value: COMPLETED
  },
  {
    label: '已取消',
    value: CANCELED
  },
  {
    label: '已退款',
    value: REFUND
  }
]
export default {
  components: {
    Modal
  },
  data () {
    return {
      WAIT_UNLOCK,
      ALREADY_UNLOCK,
      EXPERIENCING,
      COMPLETED,
      CANCELED,
      REFUND,
      visible: false,
      tableData: [],
      listTotal: null,
      games: [
        {
          label: '全部',
          value: null
        },
        {
          label: '王者荣耀',
          value: 1104466820
        },
        {
          label: '和平精英',
          value: 1106467070
        }
      ],
      orderStatusMap,
      orderStatus,
      platformType: [
        {
          label: '全部',
          value: null
        },
        {
          label: '拼多多',
          value: 0
        }
      ],
      query: {
        keyword: null,
        orderStatus: null,
        gameId: null,
        orderTime: null,
        platformType: null,
        operateUserName: null,
        pageNum: 1,
        pageSize: 10
      },
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() > Date.now()
        },
        shortcuts: [
          {
            text: '今天',
            onClick (picker) {
              picker.$emit('pick', new Date())
            }
          },
          {
            text: '昨天',
            onClick (picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24)
              picker.$emit('pick', date)
            }
          },
          {
            text: '一周前',
            onClick (picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', date)
            }
          }
        ]
      },
      dialogVisible: false,
      minute: null
    }
  },
  mounted () {
    this.fetchList()
    // this.fetchSelector()
    // console.log('test deploy 3345!!')
  },
  methods: {
    async fetchList () {
      const result = await this.$http.post(
        '/thirdOrder/thirdOrders',
        this.query
      )
      if (result.code !== '0') {
        this.$message.error(result.desc)
        return
      }
      this.tableData = result.data.list
      this.listTotal = result.data.totalRow
    },
    // 取消订单
    async cancelOrder (order, cb) {
      this.$confirm('此操作将取消该条订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const result = await this.$http.post('/thirdOrder/cancelOrder', {
            thirdOrderId: order.orderInfo.thirdOrderId
          })
          if (result.code !== '0' && !cb) {
            return this.$message.error(result.desc)
          }
          order.orderStatus = CANCELED
          this.$message({
            type: 'success',
            message: '取消成功!'
          })
          cb && cb()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    showAddTime (order) {
      this.currentOrder = order
      this.dialogVisible = true
    },
    // 加时长
    async addTime () {
      const result = await this.$http.post('/thirdOrder/addTime', {
        thirdOrderId: this.currentOrder.orderInfo.thirdOrderId,
        minute: this.minute
      })
      if (result.code !== '0') {
        return this.$message.error(result.desc)
      }
      this.fetchList()
      this.handleClose()
    },
    // 退款
    async refundOrder (order) {
      this.$confirm('此操作将为该条订单执行退款操作, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const result = await this.$http.post('/thirdOrder/refundOrder', {
            thirdOrderId: order.orderInfo.thirdOrderId
          })
          if (result.code !== '0') {
            return this.$message.error(result.desc)
          }
          order.orderStatus = REFUND
          this.$message({
            type: 'success',
            message: '退款成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },

    // 详情
    toDetail (order) {
      this.$router.push({
        path: `/eCommerce/detail/${order.orderNo}`
      })
    },
    // 详情
    toOrder (order) {
      this.$router.push({
        path: `/eCommerce/orders?orderNo=${order.orderNo}`
      })
    },
    // 查询清空排序相关字段
    search () {
      // this.query.name = null
      // this.query.sort = null
      this.query.pageNum = 1
      this.fetchList()
    },
    // 排序
    sort (item) {
      this.query.name = item.column.property
      this.query.sort = item.order === 'ascending' ? 'asc' : 'desc'
      this.fetchList()
    },
    pageChange (page) {
      this.query.pageNum = page
      this.fetchList()
      console.log(page)
    },
    sizeChange (size) {
      this.query.pageSize = size
      this.fetchList()
    },
    handleClose () {
      this.dialogVisible = false
    },
    tableRowClassName ({ row, rowIndex }) {
      if (row.orderStatus === 2) {
        return 'success-row'
      } else if (row.orderStatus === 4) {
        return 'cancel-row'
      }
      return ''
    }
  }
}
</script>
<style lang="scss" scoped>
.query {
  .add {
    float: right;
  }
}
.tools {
  margin-top: 15px;
  padding-right: 20px;
  display: flex;
  justify-content: flex-end;
}
/deep/ .btns {
  .el-button {
    margin-bottom: 6px;
  }
}

/deep/ .el-table .cancel-row {
  background: #fff1f4;
}

/deep/ .el-table .success-row {
  background: #f2ffeb;
}

a {
  color: #3aa5f3;
}
</style>
